//
//  HomeHRVTableViewCell.swift
//  DaFit
//
//  Created by 尹琼 on 2022/6/9.
//  Copyright © 2022 sylar. All rights reserved.
//

import UIKit
import MYModule

import RxCocoa
import RxSwift

public class HomeHRVCell: HomeBaseCell {

    var viewModel: HomeHRVViewModel?
    
    let unitLabel = Label()
        
    
    public override func makeUI() {
        super.makeUI()
        
        iconImagView.image = UIImage(named: "ic_home_today_hrv")
        titleLabel.text = "hrv_title".language
        unitLabel.text = "hrv_ms".language
        
        measureButton.rx.tap.subscribe(onNext: {[weak self] in
            
            BluetoothResponder.shared.willMeasurement {
                
                if let isSelected = self?.measureButton.isSelected {
                    self?.measureButton.isSelected = !isSelected
                }
                
                self?.viewModel?.automaticMeasure()

            }
            
        }).disposed(by: measureButton.rx.disposeBag)
        
        
        bgView.addSubview(hrvStackView)
        bgView.addSubview(rangeLbl)
        bgView.addSubview(chartView)
        bgView.addSubview(emptyView)
        bgView.addSubview(measureButton)
        
        hrvStackView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-15)
            make.centerY.equalTo(titleLabel)
        }
        
        rangeLbl.snp.makeConstraints { make in
            make.top.equalTo(hrvStackView.snp.bottom).offset(4)
            make.trailing.equalTo(unitLbl.snp.trailing)
        }
        
        chartView.snp.makeConstraints { make in
            make.top.equalTo(dateLabel.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-16)
            
        }
        
        emptyView.snp.makeConstraints { make in
            make.centerX.equalTo(chartView.snp.centerX)
            make.centerY.equalTo(chartView.snp.centerY).offset(-10)
        }
        
        measureButton.snp.makeConstraints { make in
            make.top.equalTo(chartView.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(15)
            make.height.equalTo(52)
            make.bottom.trailing.equalToSuperview().offset(-15)
        }
        

    }
    

    func bindButton(measureState: UIControl.State) {
        
        if measureState == .selected {
            
            measureButton.isEnabled = true
            measureButton.isSelected = true
            
        } else if measureState == .normal {
            
            measureButton.isEnabled = true
            measureButton.isSelected = false
            
        } else if measureState == .disabled {
            
            measureButton.isEnabled = false
            measureButton.isSelected = false
        }
        
    }
    
    public override func bind(to viewModel: ViewModel?) {
        super.bind(to: viewModel)
        
        guard let viewModel = viewModel as? HomeHRVViewModel else {
            return
        }
        self.viewModel = viewModel
        
        let disposeBag = DisposeBag()
        viewModel.UIReplay.subscribe(onNext: {[weak self] in
            
            self?.bindButton(measureState: $0.measureState)
            self?.dateLabel.text = $0.date
            self?.currentNumLbl.text = $0.hrvTitle
            
            self?.chartView.max = CGFloat($0.chartMaxData)
            self?.chartView.min = CGFloat($0.chartMinData)
            let titles = ["00:00",
                         "06:00",
                         "12:00",
                         "18:00",
                         "24:00"]
            self?.chartView.bind(barDatas: $0.chartDatas,
                                horizontalTitles: titles,
                                YAxisMaxValue: $0.chartMaxData,
                                YAxisMinValue: $0.chartMinData,
                                YAxisLineCount: $0.chartYAxisCount)
            self?.emptyView.isHidden = $0.hasData
            
        }).disposed(by: disposeBag)
        
        viewModel.hasDevicedReplay.subscribe(onNext: {[weak self] in
            
            self?.updateLayouts(hasDevice: $0)
            
        }).disposed(by: rx.disposeBag)
 
    }
    
    
    func updateLayouts(hasDevice: Bool?) {
        
        let hidden = hasDevice == true ? false : true
        
        measureButton.isHidden = hidden
        
     
        if !hidden {
            
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(chartView.snp.bottom).offset(20)
                make.leading.equalToSuperview().offset(15)
                make.height.equalTo(52)
                make.bottom.trailing.equalToSuperview().offset(-15)
                
            }
            
        } else {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(chartView.snp.bottom)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(0)
                make.bottom.equalToSuperview().offset(-15)
                
            }
            
        }
        
    }
    
    public lazy var emptyView: EmptyView = {
        
        let emptyView = EmptyView()
        emptyView.label.text = "hrv_no_information".language
        
        return emptyView
        
    }()
    
    
    public lazy var currentNumLbl: Label = {
        let currentNumLbl = Label()
        currentNumLbl.textAlignment = .right
        currentNumLbl.textColor = UIColor.HRV.theme
        currentNumLbl.font = Fonts.boldSystemFont(ofSize: 28)
        
        return currentNumLbl
    }()
    
    private lazy var hrvStackView: StackView = {
        let hrvStackView = StackView()
        hrvStackView.alignment = .firstBaseline
        hrvStackView.axis = .horizontal
        hrvStackView.distribution = .fill
        hrvStackView.spacing = 4
        hrvStackView.addArrangedSubview(currentNumLbl)
        hrvStackView.addArrangedSubview(unitLbl)
        return hrvStackView
    }()

    public lazy var unitLbl: Label = {
        let untilLbl = Label()
        untilLbl.textAlignment = .right
        untilLbl.textColor = UIColor.assist_222222
        untilLbl.font = Fonts.systemFont(ofSize: 14)
        untilLbl.text = "unit_hrv".language
        return untilLbl
    }()

    public lazy var rangeLbl: Label = {
        let rangeLbl = Label()
        rangeLbl.font = Fonts.systemFont(ofSize: 10)
        rangeLbl.textColor = UIColor.assist_D0D0D0
        rangeLbl.textAlignment = .right
        rangeLbl.text = "HRV"
        return rangeLbl
    }()
    
    private lazy var chartView: HeartRateChartView = {
        let view = HeartRateChartView()
        view.type = .day
        return view
    }()
    

    
    private lazy var measureButton: HomeMeasureButton = {
        let measureButton = HomeMeasureButton()
        measureButton.setTitle("hrv_tap_measure_btn_title".language, for: .normal)
        measureButton.setTitle("hrv_tap_measure_end_btn_title".language, for: .selected)
        
        let image = UIImage(named: "ic_home_today_hrv_measure")
        measureButton.setImage(image, for: .normal)
        
        let disableTitleColor = UIColor.assist_A9A9A9
        let disableImage = image?.originalImageWithColor(color: disableTitleColor)
        
        measureButton.setImage(disableImage, for: .disabled)
        measureButton.setTitleColor(disableTitleColor, for: .disabled)
        
        measureButton.setTitleColor(UIColor.assist_222222, for: .normal)
        measureButton.titleLabel?.font = Fonts.systemFont(ofSize: 14)
        measureButton.backgroundColor = UIColor.assist_F8F8F8
        measureButton.setImageTitleLayout(.imgLeft, spacing: 7)
        measureButton.cornerRadius = 14
        return measureButton
    }()

}







